#define _CRT_SECURE_NO_WARNINGS 1
#include"SL.h"

LTNode* BuyNode(LTDataType x)
{
	LTNode* NewNode = (LTNode*)malloc(sizeof(LTNode));
	if (NewNode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	NewNode->data = x;
	NewNode->next = NULL;
	NewNode->prev = NULL;
	return NewNode;
}
LTNode * ListInit()
{
	LTNode* head = BuyNode(-1);
	head->next = head;
	head->prev = head;
	return head;
}


void ListNodePushBack(LTNode* phead, LTDataType x)
{
	assert(phead);
	ListNodeInsert(phead, x);
}
void ListNodePushFront(LTNode* phead, LTDataType x)
{
	assert(phead);
	ListNodeInsert(phead->next, x);

}
void ListNodePopBack(LTNode* phead)
{
	assert(phead);
	ListNodeErase(phead->prev);
}
void ListNodePopFront(LTNode* phead)
{
	assert(phead);
	ListNodeErase(phead->next);
}


void ListNodeInsert(LTNode* pos, LTDataType x)
{
	assert(pos);
	LTNode* newnode = BuyNode(x);
	LTNode* prev = pos->prev;
	LTNode* next = pos->next;
	newnode->next = pos;
	pos->prev = newnode;
	newnode->prev = prev;
	prev->next = newnode;

}
void ListNodeErase(LTNode* pos)
{
	assert(pos);
	assert(pos->next != pos);
	LTNode* prev = pos->prev;
	LTNode* next = pos->next;
	free(pos);
	prev->next = next;
	next->prev = prev;
}
void ListNodePrint(LTNode* phead)
{
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}